
#include "stdafx.h"
#include "WicStream.h"
#include "WicBitmapEncoder.h"
#include "WicBitmapFrameEncode.h"

//==============================================================================
// CWicBitmapEncoder
//==============================================================================

CWicBitmapEncoder::CWicBitmapEncoder(REFGUID guidContainerFormat, const GUID *pguidVendor)
{
    m_pIWICBitmapEncoder = NULL;
    g_theImagingFactory->CreateEncoder(guidContainerFormat, pguidVendor, &m_pIWICBitmapEncoder);
}

CWicBitmapEncoder::~CWicBitmapEncoder()
{
    if(m_pIWICBitmapEncoder)
    {
        m_pIWICBitmapEncoder->Release();
    }
    m_pIWICBitmapEncoder = NULL;
}

CWicBitmapEncoder::CWicBitmapEncoder(CWicBitmapEncoder const & objBitmapEncoder)
{
    m_pIWICBitmapEncoder = objBitmapEncoder.m_pIWICBitmapEncoder;
    m_pIWICBitmapEncoder->AddRef();
}

CWicBitmapEncoder& CWicBitmapEncoder::operator=(CWicBitmapEncoder const & objBitmapEncoder)
{
    if(m_pIWICBitmapEncoder)
    {
        m_pIWICBitmapEncoder->Release();
    }
    m_pIWICBitmapEncoder = objBitmapEncoder.m_pIWICBitmapEncoder;
    m_pIWICBitmapEncoder->AddRef();
    return *this;
}

//==============================================================================
// IWICBitmapEncoder
//==============================================================================
HRESULT STDMETHODCALLTYPE CWicBitmapEncoder::Initialize( 
    /* [in] */ CWicStream *pStream,
    /* [in] */ WICBitmapEncoderCacheOption cacheOption = WICBitmapEncoderCacheInMemory)
{
    HRESULT hr = m_pIWICBitmapEncoder->Initialize(pStream->m_pIWICStream, cacheOption);
    return hr;
}

HRESULT STDMETHODCALLTYPE CWicBitmapEncoder::GetContainerFormat( 
    /* [out] */ __RPC__out GUID *pguidContainerFormat)
{
    return m_pIWICBitmapEncoder->GetContainerFormat(pguidContainerFormat);
}

HRESULT STDMETHODCALLTYPE CWicBitmapEncoder::GetEncoderInfo( 
    /* [out] */ __RPC__deref_out_opt IWICBitmapEncoderInfo **ppIEncoderInfo)
{
    return m_pIWICBitmapEncoder->GetEncoderInfo(ppIEncoderInfo);
}

HRESULT STDMETHODCALLTYPE CWicBitmapEncoder::SetColorContexts( 
    /* [in] */ UINT cCount,
    /* [size_is][in] */ __RPC__in_ecount_full(cCount) IWICColorContext **ppIColorContext)
{
    return m_pIWICBitmapEncoder->SetColorContexts(cCount, ppIColorContext);
}

HRESULT STDMETHODCALLTYPE CWicBitmapEncoder::SetPalette( 
    /* [in] */ CWicPalette *pIPalette)
{
    return m_pIWICBitmapEncoder->SetPalette(pIPalette->GetPtr());
}

HRESULT STDMETHODCALLTYPE CWicBitmapEncoder::SetThumbnail( 
    /* [in] */ IWICBitmapSource *pIThumbnail)
{
    return m_pIWICBitmapEncoder->SetThumbnail(pIThumbnail);
}

HRESULT STDMETHODCALLTYPE CWicBitmapEncoder::SetPreview( 
    /* [in] */ IWICBitmapSource *pIPreview)
{
    return m_pIWICBitmapEncoder->SetPreview(pIPreview);
}

HRESULT STDMETHODCALLTYPE CWicBitmapEncoder::CreateNewFrame( 
    /* [out] */ __RPC__deref_out_opt IWICBitmapFrameEncode **ppIFrameEncode,
    /* [unique][out][in] */ __RPC__deref_opt_inout_opt IPropertyBag2 **ppIEncoderOptions)
{
    return m_pIWICBitmapEncoder->CreateNewFrame(ppIFrameEncode, ppIEncoderOptions);
}

HRESULT STDMETHODCALLTYPE CWicBitmapEncoder::Commit()
{
    return m_pIWICBitmapEncoder->Commit();
}

HRESULT STDMETHODCALLTYPE CWicBitmapEncoder::GetMetadataQueryWriter( 
    /* [out] */ __RPC__deref_out_opt IWICMetadataQueryWriter **ppIMetadataQueryWriter)
{
    return m_pIWICBitmapEncoder->GetMetadataQueryWriter(ppIMetadataQueryWriter);
}

